package com.example.demo.Controller;

import com.example.demo.pojo.Furn;
import com.example.demo.pojo.Member;
import com.example.demo.service.MemberService;
import com.example.demo.service.furnService.FurnService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.google.code.kaptcha.impl.DefaultKaptcha;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;

@Controller
@RequestMapping("/member")
public class memberController {
    @Resource
    private MemberService service;
    @Resource
    DefaultKaptcha defaultKaptcha;
    @Resource
    FurnService furnService;

//    @GetMapping({"/index","/"})
//    public String showIndex(@RequestParam(defaultValue = "1") Integer pageNum, @RequestParam(defaultValue = "4") Integer pageSize,HttpSession session,Model model){
//
//        PageHelper.startPage(pageNum,pageSize);
//        List<Furn> allFurn = furnService.findAllFurn();
//        System.out.println("show:"+allFurn);
//        PageInfo pageInfo = new PageInfo(allFurn, pageSize);
//
//        session.setAttribute("pageInfo",pageInfo);
//        session.setAttribute("allFurns",allFurn);
//        model.addAttribute("pageInfo",pageInfo);
//        model.addAttribute("allFurn",allFurn);
//        return  "index";
//    }
    @GetMapping("/login")
   public  String showLogin(){
        return  "login";
    }
    @GetMapping("/indexToLogin")
    public String showLogin1(){
        return  "login";
    }

    @PostMapping("/loginuser")
    public String register(@RequestParam("user-name")String name , @RequestParam("user-password")String password, HttpServletRequest request){
        Member admin = service.findMember(name,password);
        request.getSession().setAttribute("member",admin);
        if (admin!=null){
             request.getSession().setAttribute("username",name);
            return "login_ok";
        }
        else {
            request.getSession().setAttribute("errorMsg","用户不存在");
            return "login";
        }
    }
    @PostMapping("/register")
    public String register(Member member,@RequestParam("code") String code,HttpSession session){
        String imageCode = session.getAttribute("imageCode").toString();
        System.out.println("ok");
        if (code.equals(imageCode)) {
            //验证码通过

            boolean b = service.insertMember(member);
            if (b){
                return  "register_ok";
            }else
            {
                return  "register_fail";
            }
        }else {

            session.setAttribute("errorMsg","验证码不正确");
            return  "login";
        }


    }
    @GetMapping("/manage_menu")
    public String manageMenu(){

        return  "manage_menu";
    }
    @GetMapping("/validateMember")
    @ResponseBody
    public HashMap<String,Object> isHaveMember(String yonghuName){
        HashMap<String, Object> map = new HashMap<>();
        boolean haveMember = service.isHaveMember(yonghuName);
        if (haveMember){
            map.put("isExist",haveMember);
         return map;
      }
        else {
            map.put("isExist",null);
            return  map;
      }
    }


    @GetMapping("/manger")
    public String  mangerView(){

        return "manage_login";
    }
    //生成图片验证码
    @GetMapping("/createImageCode")
    public void createImageCode(HttpServletRequest request, HttpServletResponse response, HttpSession session) {
        response.setHeader("Cache-Control", "no-store, no-cache");
        response.setContentType("image/jpeg");
        // 生成文字验证码
        String text = defaultKaptcha.createText();
        // 生成图片验证码
        BufferedImage image = defaultKaptcha.createImage(text);
//        //保存到session域
       session = request.getSession();
        session.setAttribute("imageCode",text);
        ServletOutputStream out = null;
        try {
            //响应输出图片流
            out = response.getOutputStream();
            ImageIO.write(image, "jpg", out);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                out.flush();
                out.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    @GetMapping("/loginout")

    public  String  loginOut(HttpSession session){
        session.invalidate();

        return   "redirect:index";
    }
    @GetMapping({"/index","/"})
    public String searchFurn(@RequestParam(defaultValue = "1") Integer pageNum, @RequestParam(defaultValue = "4") Integer pageSize,HttpSession session,Model model,
                             @RequestParam(defaultValue = "") String furnName){

        PageHelper.startPage(pageNum,pageSize);
        List<Furn> allFurn = furnService.searchFurn(furnName);
        System.out.println(allFurn);

        PageInfo pageInfo = new PageInfo(allFurn, pageSize);

        session.setAttribute("pageInfo",pageInfo);
        session.setAttribute("allFurns",allFurn);
        session.setAttribute("furnName",furnName);
        model.addAttribute("pageInfo",pageInfo);
        model.addAttribute("allFurn",allFurn);
        return  "index";
    }



}
